package com.shareloke.server.handler

import com.google.gson.Gson
import com.shareloke.server.netty.model.SocketMessage
import com.shareloke.server.tools.MyChannelManager
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.SimpleChannelInboundHandler
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame

/**
 *
 *  描述：
 *
 * @author wlp
 * @version 1.0.0
 * @date 2021/7/13 上午 09:56
 */
class MyWebSocketServerHandler : SimpleChannelInboundHandler<TextWebSocketFrame>() {

    @Throws(Exception::class)
    override fun channelRead0(ctx: ChannelHandlerContext, textWebSocketFrame: TextWebSocketFrame) {
        val msgText = textWebSocketFrame.text()
        val socketMessage = Gson().fromJson(msgText, SocketMessage::class.java)
        val info = "后端接收的信息----》${MyChannelManager.getUserId(ctx.channel())}  : $socketMessage"
        println(info)
        val socketMessage1 = SocketMessage(info, "消息反馈", socketMessage.receiveId)
        if ("0" == socketMessage.receiveId)
            MyChannelManager.sendAll(socketMessage1)
        else {
            MyChannelManager.sendOne(socketMessage1, socketMessage.receiveId)
        }
    }
}